package com.dingpeikeji.quickdeer.modules.rentproduct.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.springframework.ui.Model;

import com.dingpeikeji.quickdeer.core.common.persistence.DataEntity;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 发布整车Entity
 * @author wuhao
 * @version 2016-08-08
 */
public class TRentproduct extends DataEntity<TRentproduct> {
	
	private static final long serialVersionUID = 1L;
	
	private Integer rentid;		// 主键
	private Integer	rentgrpid;	
	private Office office;		// 部门
	private String productname;		// 产品名称
	private String fixedprice; // 是否是一口价
	private TCargotype cargotype; // 车型
	private TLine	line;			//线路
	private TSitedtl departcity;		// 出发城市
	private TSitedtl destcity;		// 到达城市
	private Date fromdate;		// 租赁起期
	@JsonIgnore
	private Date stopdate;		// 租赁止期
	@JsonIgnore
	private String fromdateStr;		// 租赁起期
	@JsonIgnore
	private String stopdateStr;		// 租赁止期
	
	private String payway;		// 支付方式
	private Double suitrtnprice;		// 套餐价格（零售往返价）
	private Double suitonewayprice;		// 套餐价格（零售单程价）
	private Double suitfreeprice;		// 套餐价格（零售顺风价）
	private Double kms;		// 公里数
	private Double lasthour;		// 时长（小时）
	private String isroadtoll;		// 包含过路过桥费
	private String isparkfee;		// 包含停车费
	private Double outtimefee;		// 超时长费
	private Double outkmsfee;		// 超公里费
	private String suitdesc;		// 套餐描述
	private Integer vehiclenum;		// 车辆数
	private String releasegroup;		// 发布对象一
	private String releaseobject;		// 发布对象二
	private Integer seats; // 座位数
	private Integer packs; // 行李数
	private Double nightquota; // 夜间服务费比例
	private String isall; // 是否针对全部合作伙伴
	private String ispublic; // 是否发布给公众
	private String iselectric; //是否是电动车
	private Integer	continuerange;//续航里程
	private Double	nonoccu;	//空驶费
	private String	brand;		//品牌信息
	private Integer daysnum; // 产品天数
	private String rentType; // 整车类型
	private String auditstatus; // 审核字段
	private String description; // 线路描述
	private String mapkm; // 线路描述
	private String maptime; // 线路描述
	private List<TCargotype> cargotypeList;//线路所拥有的车型
	// 拓展字段
	@JsonIgnore
	private Integer sorts;		//排序字段
	@JsonIgnore
	private String compAgreerateOfficeId; // 协议价所属部门ID查询条件
	private String tabType; // 按天包车tab页（市内、跨市）
	private String anothertabType;//另一个tabtype
	@JsonIgnore
	private String time; // 出发时间
	@JsonIgnore
	private Date departdate;	// 出发日期，fromdataStr+time
	@JsonIgnore
	private Date sysDate; // 系统当前时间
	private String aboardAddress;//接送机上车地点
	public TRentproduct() {
		super();
	}

	public String getMapkm() {
		return mapkm;
	}

	public void setMapkm(String mapkm) {
		this.mapkm = mapkm;
	}

	public String getAnothertabType() {
		return anothertabType;
	}

	public void setAnothertabType(String anothertabType) {
		this.anothertabType = anothertabType;
	}

	public String getAboardAddress() {
		return aboardAddress;
	}

	public void setAboardAddress(String aboardAddress) {
		this.aboardAddress = aboardAddress;
	}

	public String getMaptime() {
		return maptime;
	}

	public void setMaptime(String maptime) {
		this.maptime = maptime;
	}

	public TRentproduct(String id){
		super(id);
	}

	public TCargotype getCargotype() {
		return cargotype;
	}


	public void setCargotype(TCargotype cargotype) {
		this.cargotype = cargotype;
	}

	public Integer getRentid() {
		return rentid;
	}

	public List<TCargotype> getCargotypeList() {
		return cargotypeList;
	}

	public void setCargotypeList(List<TCargotype> cargotypeList) {
		this.cargotypeList = cargotypeList;
	}

	public void setRentid(Integer rentid) {
		this.rentid = rentid;
	}
	
	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}
	
	@Length(min=1, max=50, message="产品名称长度必须介于 1 和 50 之间")
	public String getProductname() {
		return productname;
	}

	public void setProductname(String productname) {
		this.productname = productname;
	}
	
	public TSitedtl getDepartcity() {
		return departcity;
	}

	public void setDepartcity(TSitedtl departcity) {
		this.departcity = departcity;
	}
	
	public TSitedtl getDestcity() {
		return destcity;
	}

	public void setDestcity(TSitedtl destcity) {
		this.destcity = destcity;
	}
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@NotNull(message="租赁起期不能为空")
	public Date getFromdate() {
		return fromdate;
	}

	public void setFromdate(Date fromdate) {
		this.fromdate = fromdate;
	}
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@NotNull(message="租赁止期不能为空")
	public Date getStopdate() {
		return stopdate;
	}

	public void setStopdate(Date stopdate) {
		this.stopdate = stopdate;
	}
	
	@Length(min=1, max=1, message="支付方式长度必须介于 1 和 1 之间")
	public String getPayway() {
		return payway;
	}

	public void setPayway(String payway) {
		this.payway = payway;
	}
	
	@NotNull(message="套餐价格（零售往返价）不能为空")
	public Double getSuitrtnprice() {
		return suitrtnprice;
	}

	public void setSuitrtnprice(Double suitrtnprice) {
		this.suitrtnprice = suitrtnprice;
	}
	
	public Double getSuitonewayprice() {
		return suitonewayprice;
	}

	public void setSuitonewayprice(Double suitonewayprice) {
		this.suitonewayprice = suitonewayprice;
	}
	
	public Double getSuitfreeprice() {
		return suitfreeprice;
	}

	public void setSuitfreeprice(Double suitfreeprice) {
		this.suitfreeprice = suitfreeprice;
	}
	
	public Double getKms() {
		return kms;
	}

	public void setKms(Double kms) {
		this.kms = kms;
	}
	
	public Double getLasthour() {
		return lasthour;
	}

	public void setLasthour(Double lasthour) {
		this.lasthour = lasthour;
	}
	
	@Length(min=1, max=1, message="包含过路过桥费长度必须介于 1 和 1 之间")
	public String getIsroadtoll() {
		return isroadtoll;
	}

	public void setIsroadtoll(String isroadtoll) {
		this.isroadtoll = isroadtoll;
	}
	
	@Length(min=1, max=1, message="包含停车费长度必须介于 1 和 1 之间")
	public String getIsparkfee() {
		return isparkfee;
	}

	public void setIsparkfee(String isparkfee) {
		this.isparkfee = isparkfee;
	}
	
	public Double getOuttimefee() {
		return outtimefee;
	}

	public void setOuttimefee(Double outtimefee) {
		this.outtimefee = outtimefee;
	}
	
	public Double getOutkmsfee() {
		return outkmsfee;
	}

	public void setOutkmsfee(Double outkmsfee) {
		this.outkmsfee = outkmsfee;
	}
	
	@Length(min=0, max=200, message="套餐描述长度必须介于 0 和 200 之间")
	public String getSuitdesc() {
		return suitdesc;
	}

	public void setSuitdesc(String suitdesc) {
		this.suitdesc = suitdesc;
	}
	
	@NotNull(message="车辆数不能为空")
	public Integer getVehiclenum() {
		return vehiclenum;
	}

	public void setVehiclenum(Integer vehiclenum) {
		this.vehiclenum = vehiclenum;
	}
	
	@Length(min=0, max=200, message="发布对象一长度必须介于 0 和 200 之间")
	public String getReleasegroup() {
		return releasegroup;
	}

	public void setReleasegroup(String releasegroup) {
		this.releasegroup = releasegroup;
	}
	
	@Length(min=0, max=2000, message="发布对象二长度必须介于 0 和 2000 之间")
	public String getReleaseobject() {
		return releaseobject;
	}

	public void setReleaseobject(String releaseobject) {
		this.releaseobject = releaseobject;
	}

	public Double getNightquota() {
		return nightquota;
	}

	public void setNightquota(Double nightquota) {
		this.nightquota = nightquota;
	}

	@Length(min=1, max=1, message="是否针对全部合作伙伴长度必须为1")
	public String getIsall() {
		return isall;
	}

	public void setIsall(String isall) {
		this.isall = isall;
	}

	@Length(min=1, max=1, message="是否发布给公众长度必须为1")
	public String getIspublic() {
		return ispublic;
	}

	public void setIspublic(String ispublic) {
		this.ispublic = ispublic;
	}

	public String getFromdateStr() {
		return fromdateStr;
	}

	public void setFromdateStr(String fromdateStr) {
		this.fromdateStr = fromdateStr;
	}

	public String getStopdateStr() {
		return stopdateStr;
	}

	public void setStopdateStr(String stopdateStr) {
		this.stopdateStr = stopdateStr;
	}

	@NotNull(message="一口价不能为空")
	public String getFixedprice() {
		return fixedprice;
	}

	public void setFixedprice(String fixedprice) {
		this.fixedprice = fixedprice;
	}

	public Integer getSeats() {
		return seats;
	}

	public void setSeats(Integer seats) {
		this.seats = seats;
	}

	public Integer getPacks() {
		return packs;
	}

	public void setPacks(Integer packs) {
		this.packs = packs;
	}

	public String getRentType() {
		return rentType;
	}

	public void setRentType(String rentType) {
		this.rentType = rentType;
	}

	public String getIselectric() {
		return iselectric;
	}

	public void setIselectric(String iselectric) {
		this.iselectric = iselectric;
	}

	public Double getNonoccu() {
		return nonoccu;
	}

	public void setNonoccu(Double nonoccu) {
		this.nonoccu = nonoccu;
	}

	public Integer getContinuerange() {
		return continuerange;
	}

	public void setContinuerange(Integer continuerange) {
		this.continuerange = continuerange;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}


	public Integer getDaysnum() {
		return daysnum;
	}

	public void setDaysnum(Integer daysnum) {
		this.daysnum = daysnum;
	}

	public String getCompAgreerateOfficeId() {
		return compAgreerateOfficeId;
	}

	public void setCompAgreerateOfficeId(String compAgreerateOfficeId) {
		this.compAgreerateOfficeId = compAgreerateOfficeId;
	}

	public String getAuditstatus() {
		return auditstatus;
	}

	public void setAuditstatus(String auditstatus) {
		this.auditstatus = auditstatus;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getRentgrpid() {
		return rentgrpid;
	}

	public void setRentgrpid(Integer rentgrpid) {
		this.rentgrpid = rentgrpid;
	}

	public TLine getLine() {
		return line;
	}

	public void setLine(TLine line) {
		this.line = line;
	}

	public String getTabType() {
		return tabType;
	}

	public void setTabType(String tabType) {
		this.tabType = tabType;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
	
	public Date getDepartdate() {
		return departdate;
	}

	public void setDepartdate(Date departdate) {
		this.departdate = departdate;
	}

	public Date getSysDate() {
		return sysDate;
	}

	public void setSysDate(Date sysDate) {
		this.sysDate = sysDate;
	}

	// 日期格式
	private SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat ymdmhsSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 如果发车日期为空或者小于当前日期，则设置成明天
	 * @param model
	 * @param ymdSdf
	 * @throws ParseException 
	 */
	public TRentproduct handleFromDate(Model model) throws ParseException{
		// 当前系统日期
		this.sysDate = new Date();
		// 当前系统日期“年月日”字符串
		String currDateYmdStr = this.ymdSdf.format(this.sysDate);
		model.addAttribute("currDateYmdStr", currDateYmdStr);
		if (ValidateTool.isEmptyOrNull(this.fromdateStr) || this.fromdateStr.compareTo(currDateYmdStr) < 0) {
			Calendar tomorrowCal = Calendar.getInstance();
			tomorrowCal.add(Calendar.DAY_OF_MONTH, 1);
			this.fromdate = tomorrowCal.getTime();
			this.fromdateStr = this.ymdSdf.format(this.fromdate);
		} else {
			this.fromdate = this.ymdSdf.parse(this.fromdateStr);
		}
		// 发车时间非空，默认上午6点
		if(ValidateTool.isEmptyOrNull(this.time)) {
			this.time = "06:00";
		}
		this.departdate = this.ymdmhsSdf.parse(this.fromdateStr+" "+this.time+":00");
		return this;
	}
	
	/**
	 * 发车时间-2<=当前时间
	 * @return
	 * @throws ParseException 
	 */
	public String handle2HoursWarning() throws ParseException {
		Long fromdateLong = this.departdate.getTime() - 60*60*1000*2;
		if(new Date(fromdateLong).before(this.sysDate)){
			return "距离发车时间不足2小时，请重新选择发车时间";
		} 
		return null;
	}
	
	/**
	 * 发车时间 - 12<=当前时间，添加警告信息
	 * @param model
	 */
	public TRentproduct handle12HoursWarning(Model model) {
		Long fromdate12Long = this.departdate.getTime() - 60*60*1000*12;
		if(new Date(fromdate12Long).before(this.sysDate)){
			model.addAttribute("messagesFor12Hour", "当前查询时间距离发车时间已不足12小时，为保证您的顺利出行，请保持电话畅通");
		}
		return this;
	}
	
	/**
	 *  包车天数为空，1天-10天选择，默认1天
	 * @return
	 */
	public TRentproduct handleDaysnum(){
		if(this.daysnum == null){
	//		this.daysnum = 1;
		}
		return this;
	}
	
	/**
	 * 根据包车天数计算租赁结束日期
	 * 只能搜索3个月内的产品
	 * @param ymdSdf
	 * @return
	 */
	public String handle3MonthWarning(){
		Calendar fromdateCal = Calendar.getInstance();
		fromdateCal.setTime(this.departdate);
/*		fromdateCal.add(Calendar.DAY_OF_MONTH, this.daysnum);
		if(this.rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_DAY) ||
				this.rentType.equals(TOrders.ORDER_TYPE_REALSCHE_SELF_TRAVEL) ||
						this.rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_CUSTOM)){
			this.stopdateStr = this.ymdSdf.format(fromdateCal.getTime());
		}*/
		Calendar threeMonthCal = Calendar.getInstance();
		threeMonthCal.add(Calendar.MONTH, +3);
		// 只能搜索3个月内的产品
		if(fromdateCal.getTime().after(threeMonthCal.getTime())) {
			return "只能搜索3个月内的产品";
		}
		return null;
		
	}
	
	/**
	 * 当前下午2点停止卖票
	 * @return
	 */
	public String handle14HoursWarning() {
		// TODO 此处注释是为了方便测试，正式使用时需要去掉注释
		/*Calendar cal = Calendar.getInstance();
		if(cal.get(Calendar.HOUR_OF_DAY) >= 14) {
			return "抱歉！每日14：00后只能预定后天车辆，为保障您的顺利出行，请拨打平台客服电话4009287733，转人工预定。";
		}*/
		return null;
	}

	public Integer getSorts() {
		return sorts;
	}

	public void setSorts(Integer sorts) {
		this.sorts = sorts;
	}
	
}